package com.tianqu.tms.core.util; 
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import com.tianqu.tms.core.base.Logger;

import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileInputStream; 
 
/**
 * @author HuangWei st2
 *
 */
public class JCifsUtil { 
	private static Logger LOGGER = Logger.getLogger(JCifsUtil.class);//log
    
    /**
     * huangwei
     * @param smbFile
     * @param fileName
     * @param localDirectory
     * @return
     */
    private static boolean copyRemoteFile(SmbFile smbFile,String fileName, String localDirectory) { 
        SmbFileInputStream in = null; 
        FileOutputStream out = null; 
        try { 
            File[] localFiles = new File(localDirectory).listFiles(); 
            if(null == localFiles) new File(localDirectory).mkdirs(); 
            in = new SmbFileInputStream(smbFile); 
            out = new FileOutputStream(localDirectory + fileName); 
            byte[] buffer = new byte[1024]; 
            int len = -1; 
            while((len=in.read(buffer)) != -1){ 
                out.write(buffer, 0, len); 
            } 
        } catch (Exception e) { 
        	LOGGER.error("failed to copyRemoteFile,", e);
            return false; 
        } finally { 
            if(null != out){ 
                try { 
                    out.close(); 
                } catch (IOException e) {} 
            } 
            if(null != in){ 
                try { 
                    in.close(); 
                } catch (IOException e) {} 
            } 
        } 
        return true; 
    } 
  
    /**
     * huangwei
     * @param remoteUsername
     * @param remotePassword
     * @param remoteFilepath
     * @param localDirectory
     * @return
     */
    public static boolean getRemoteFile(String remoteUsername, String remotePassword, String remoteFilepath, String localDirectory,String fileName) { 
        boolean isSuccess = false; 
        if(remoteFilepath.startsWith("/") || remoteFilepath.startsWith("\\"))  return isSuccess; 
        if(!(localDirectory.endsWith("/") || localDirectory.endsWith("\\"))) return isSuccess; 
        
        try {
        	SmbFile smbFile ;
        	if(!StringUtil.isNullOrBlank(remoteUsername) && !StringUtil.isNullOrBlank(remotePassword))
        		smbFile = new SmbFile("smb://" + remoteUsername + ":" + remotePassword + "@" + remoteFilepath); 
        	else smbFile = new SmbFile("smb://"+ remoteFilepath); 
           
            if(smbFile.isDirectory()){ 
                for(SmbFile file : smbFile.listFiles()){ 
                    isSuccess = copyRemoteFile(file,fileName,localDirectory); 
                } 
            }else if(smbFile.isFile()){ 
                isSuccess = copyRemoteFile(smbFile,fileName,localDirectory); 
            } 
        } catch (Exception e) { 
        	LOGGER.error("failed to getRemoteFile,", e);
        } 
        return isSuccess; 
    } 
    
    public static void main(String[] args) { 
    	String path = "192.168.2.220\\2015\\08\\27\\20150827155327047966.jpg";
        getRemoteFile("Administrator", "123", path.replace("\\","/"), "D:/mylocal/","20150827155327047222966.jpg"); 
    } 
}